<template>
  <div class="my-register">
    <div class="form">
      <el-card v-loading="loading">
        <div class="title">注册</div>
        <el-form label-position="left" ref="form" :model="form" label-width="80px" size>
          <el-form-item label="账号">
            <el-input v-model="form.account" placeholder="请输入账号"></el-input>
          </el-form-item>
          <el-form-item label="密码">
            <el-input v-model="form.pwd" placeholder="密码" type="password"></el-input>
          </el-form-item>
          <el-form-item label="验证密码">
            <el-input v-model="form.pwd2" placeholder="重复输入密码" type="password"></el-input>
          </el-form-item>
          <el-form-item label="手机号">
            <el-input v-model="form.tel" placeholder="请输入手机号" type="text"></el-input>
          </el-form-item>

          <el-form-item label="验证码">
            <el-col :span="8">
              <el-input v-model="form.code" placeholder="验证码" type="text"></el-input>
            </el-col>
            <el-col :span="15" :offset="1">
              <el-button type="primary" size v-loading="sendLoading" @click="getCode()" v-if="time<=0" >发送短信验证码</el-button>
              <el-button type="info" disabled v-else>已发送:{{time}}</el-button>
            </el-col>
          </el-form-item>

          <el-form-item label="生日">
            <el-date-picker
              type="date"
              placeholder="选择日期"
              v-model="form.birthday"
              style="width: 100%;"
            ></el-date-picker>
          </el-form-item>

          <el-form-item label="性别">
            <el-radio-group v-model="form.sex">
              <el-radio label="1">男</el-radio>
              <el-radio label="2">女</el-radio>
              <el-radio label="0">保密</el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="onSubmit">立即注册</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
import qs from "qs";
export default {
  name: "my-register",
  data() {
    return {
      form: {
        account: "",
        pwd: "",
        pwd2: "",
        birthday: "",
        sex: "",
        tel: "",
        code: ""
      },
      loading: false,
      sendLoading: false,
      time:0,
      timer:null
    };
  },
  methods: {
    onSubmit() {
      console.log(this.form);
      this.loading = true;
      this.axios
        .post("user/register", this.form)
        .then(response => {
          this.loading = false;
          if (response.data.success) {
            this.$alert("恭喜您，注册成功", "提示", {
              confirmButtonText: "跳转到登录页面",
              callback: action => {
                this.$router.replace({ name: "login" });
              }
            });
          }
        })
        .catch(function(error) {
          this.loading = false;
          console.log(error);
        });
    },
    getCode() {
      if (this.form.tel.length < 11) {
        this.$message.error("请输入正确的手机号！");
      }
      this.sendLoading = true;
      this.axios
        .post("user/getCode", {
          tel: this.form.tel
        })
        .then(res => {
          this.sendLoading = false;
          this.form.token = res.data.token;
          this.countDown(30)
        });
    },
    countDown(time){
       this.time=time
       this.timer=setInterval(()=>{
        this.time--
      },1000);
    }
  },
  watch:{
    time:function(val){
      if(val<=0)
        clearInterval(this.timer)
    }
  }
};
</script>

<style lang="scss" scoped>
.my-register {
  height: 900px;
  margin-top: 10px;
  .form {
    width: 30%;
    margin: 0 auto;
    height: 600px;
    .title {
      text-align: center;
      font-size: 30px;
      line-height: 60px;
      font-weight: bold;
      color: #909399;
    }
    .el-button {
      width: 100%;
    }
    .find-pwd {
      margin-left: 2px;
      color: #c0c4cc;
    }
  }
}
</style>    